package com.wm.service.imp;

import java.util.Date;
import java.util.List;

import com.wm.bean.Restaurant;
import com.wm.bean.Shopkeeper;
import com.wm.bean.User;
import com.wm.comm.PinYinUtil;
import com.wm.dao.AddressDAO;
import com.wm.dao.RestaurantDAO;
import com.wm.dao.ShopkeeperDAO;
import com.wm.dao.UserDAO;
import com.wm.service.ShopkeeperService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

/**
 * @author yangbo
 *
 * 2013-1-4
 */
public class ShopkeeperServiceImp implements ShopkeeperService {

	private ShopkeeperDAO shopkeeperDAO;
	private RestaurantDAO restaurantDAO;

	public ShopkeeperDAO getShopkeeperDAO() {
		return shopkeeperDAO;
	}

	public void setShopkeeperDAO(ShopkeeperDAO shopkeeperDAO) {
		this.shopkeeperDAO = shopkeeperDAO;
	}

	public RestaurantDAO getRestaurantDAO() {
		return restaurantDAO;
	}

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	/**
	 * 保存
	 * @param sk
	 * @return
	 */
	public Shopkeeper saveShopkeeper(Shopkeeper sk){
		return this.getShopkeeperDAO().saveShopkeeper(sk);
	}
	
	/**
	 * 修改
	 * @param sk
	 * @return
	 */
	public Shopkeeper updateShopkeeper(Shopkeeper sk){
		sk = this.getShopkeeperDAO().updateShopkeeper(sk);
		if(sk.getStatus() == 1){
			Restaurant restaurant = this.getRestaurantDAO().findRestaurantById(sk.getId());
			if(restaurant == null){
				restaurant = new Restaurant();
				restaurant.setId(sk.getId());
				restaurant.setResName(" ");
				restaurant.setResInitials(" ");
				restaurant.setResAnglic(" ");
				restaurant.setResPhonetic(" ");
				restaurant.setResTaste(" ");
				restaurant.setIsCatenation(0);
				restaurant.setDeliveryPrice(0);
				restaurant.setSpecialMemo(" ");
				restaurant.setSummary(" ");
				restaurant.setAddress(" ");
				restaurant.setDistance(0);
				restaurant.setNotice(" ");
				restaurant.setSalesPromotion(" ");
				restaurant.setScore(0);
				restaurant.setScorePeople(0);
				restaurant.setValid(0);
				restaurant.setIsShow(0);
				restaurant.setShowTime((new Date()).getTime());
				restaurant.setOrderNum(0);
				restaurant.setOrderTime(new Date());
				restaurant.setAddTime(new Date());
				restaurant.setDeliverTime(0);
				restaurant.setPhone(" ");
				restaurant.setPhone2(" ");
				restaurant.setLogo(" ");
				this.getRestaurantDAO().saveRestaurant(restaurant);
			}
		}
		if(sk.getStatus() == 2){
			Restaurant restaurant = this.getRestaurantDAO().findRestaurantById(sk.getId());
			if(restaurant != null){
				restaurant.setValid(2);
				restaurant.setIsShow(0);
				this.getRestaurantDAO().updateRestaurant(restaurant);
			}
		}
		return sk;
	}
	
	/**
	 * 根据ID删除Shopkeeper对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeShopkeeperById(long id){
		this.getShopkeeperDAO().removeShopkeeperById(id);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	public Shopkeeper findShopkeeperById(long id){
		return this.getShopkeeperDAO().findShopkeeperById(id);
	}
	
	/**
	 * 根据用户名查询
	 * @param userName
	 * @return
	 */
	public Shopkeeper findShopkeeperByUserName(String userName){
		return this.getShopkeeperDAO().findShopkeeperByUserName(userName);
	}
	
	public PageList findShopkeepersByUserNameStatus(String userName,int status,String orderby,int ascOrDesc,Pages pages){
		PageList pl = new PageList();
        if (pages.getTotalNum() == -1) {
            pages.setTotalNum(this.getShopkeeperDAO().getShopkeeperNumByUserNameStatus(userName, status));
        }
        pages.executeCount();
        if(pages.getTotalNum() == 0){
        	pl.setObjectList(null);
        	pl.setPages(pages);
        	return pl;
        }
        List l = this.getShopkeeperDAO().findShopkeepersByUserNameStatus(userName, status,orderby, ascOrDesc, pages.getSpage(), pages.getPerPageNum());
        if(l == null || l.isEmpty()){
	   		 pl.setObjectList(null);
	   	     pl.setPages(pages);
	   		 return pl;
		}else{
			pl.setObjectList(l);
	        pl.setPages(pages);
	        return pl;
		} 
	}
}
